﻿using System;
using System.Collections.Generic;
using System.IO;
using System.IO.Pipes;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace PipeClient
{
    public class NamedPipeClient : IDisposable
    {
        NamedPipeClientStream pipe;

        public NamedPipeClient(string pipeName)
        {
            pipe = new NamedPipeClientStream(".", pipeName, PipeDirection.InOut);
        }

        public void Connect()
        {
            Console.WriteLine("");
            pipe.Connect();
        }

        public void Write(byte[] buffer)
        {
            Write(buffer, 0, buffer.Length);
        }

        public void Write(byte[] buffer, int offset, int count)
        {
            pipe.Write(buffer, offset, count);
        }

        public int Read(byte[] buffer)
        {
            return Read(buffer, 0, buffer.Length);
        }

        public int Read(byte[] buffer, int offset, int count)
        {
            return pipe.Read(buffer, offset, count);
        }

        public void Dispose()
        {
            try
            {
                pipe.Close();
            }
            catch (Exception)
            {

            }
        }
    }
}
